﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Messaging;

namespace MSMQWinform
{
    public class QueueManager
    {
        public static MessageQueue GetQueue(string queuePath, bool isFormat)
        {
            if (string.IsNullOrEmpty(queuePath)) { return null; }
            MessageQueue m_MessageQueue = null;
            try
            {
                if (isFormat)
                {
                    m_MessageQueue = new MessageQueue(queuePath);
                }
                else if (MessageQueue.Exists(queuePath))
                {
                    m_MessageQueue = new MessageQueue(queuePath);
                }
            }
            catch { }
            return m_MessageQueue;
        }

        ///创建队列：
        ///创建本机队列：@".\private$\队列名称"
        ///创建远程队列：@"FormatName:DIRECT=TCP:远程机器IP\private$\队列名称"
        public static MessageQueue CreateQueue(string queuePath, bool isFormat)
        {
            if (string.IsNullOrEmpty(queuePath)) { return null; }
            MessageQueue m_MessageQueue = null;
            try
            {
                if (isFormat)
                {
                    m_MessageQueue = MessageQueue.Create(queuePath);
                }
                else
                {
                    if (!MessageQueue.Exists(queuePath))
                    {
                        m_MessageQueue = MessageQueue.Create(queuePath);
                    }
                    else
                    {
                        throw new Exception("队列已经存在!");
                    }
                }
            }
            catch (Exception e) { throw e; }

            return m_MessageQueue;
        }

        public static bool CheckQueueExists(string queueName, bool isFormat)
        {
            if (string.IsNullOrEmpty(queueName)) { return false; }
            if (isFormat) { return true; }

            return MessageQueue.Exists(queueName);
        }

        public static bool SendMsg<T>(string queueName, T sendObject, bool isFormat)
        {
            try
            {
                MessageQueue queue = QueueManager.GetQueue(queueName, isFormat);
                if (queue != null)
                {
                    Message sendMsg = new Message(sendObject, new XmlMessageFormatter(new Type[] { typeof(T) }));
                    queue.Send(sendMsg);
                    queue.Dispose();
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex) { throw ex; }
        }

        public static Message ReceiveMsg<T>(string queueName, bool isFormat)
        {
            Message m_Message = null;
            try
            {
                MessageQueue m_MessageQueue = GetQueue(queueName, isFormat);
                if (m_MessageQueue != null)
                {
                    m_MessageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(T) });
                    m_Message = m_MessageQueue.Receive();
                    m_MessageQueue.Dispose();
                }
            }
            catch
            {
                throw;
            }
            return m_Message;
        }
    }
}
